<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="tips">测试postMessage，5s后自动关闭，并发送数据</div>
<script>
    console.log('postMessage');
    let num = 5;
    let tips = document.getElementById('tips');

    function updateTips (value) {
        tips.innerText = `测试postMessage，${value}s后自动关闭，并发送数据`;
    }

    if (window.opener) {
        updateTips(num);
        let timer = setInterval(() => {
            num--;
            updateTips(num);
            if (num < 0) {
                clearInterval(timer);
                console.warn('发送消息');
                window.opener.postMessage({ action: 'login-success', data: 111 }, '*');
                window.close();
            }
        }, 1000);
    } else {
        tips.innerText = '未找到window.opener';
    }
</script>
</body>

</html>
